package p.ithorns.integration.transform.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import p.ithorns.tools.translator.annotation.Translatable;
import p.ithorns.tools.translator.annotation.field.BeanField;
import p.ithorns.tools.translator.annotation.meta.BeanDictMeta;

import java.util.List;

/**
 * User
 *
 * @author Ht.L
 * @version 1.0.0
 * @date 2023/4/5 19:05
 */
//@DictGen
@Data
@TableName("`user`")
public class User {

    @BeanDictMeta(bean = "userService", label = "userName")
    // @DictMeta(type = DictType.EXP, label = "userName", expr1 = "@userService.getDict(#key)", expr2 = "@userService.getDictMap(#keys)")
    @TableId(type = IdType.AUTO)
    private Integer id;

    @BeanField(from = "id", bean = "userService")
    private String name;

    private String gender;

    @Translatable
    @TableField(exist = false)
    private Account account;

    @Translatable
    @TableField(exist = false)
    private List<Order> orders;

    @Override
    public String toString() {
        return "{" +
                "id:" + id +
                ", name:'" + name + // "-" + userName +
                ", account:" + account +
                ", orders:" + orders +
                "}";
    }


    /**
     * 验证对象在属性值变化后hashCode改变的问题
     * 经测试，是lombok的@Data注解导致的
     * 通过使用@Data注解，可以自动生成getter、setter、toString、equals和hashCode等方法
     * 故当属性值变化时，hashCode也会变化
     *
     * @see p.ithorns.tools.translator.sdk.utils.ReflectUtil#makeBeanFieldRef
     */
    public static void main(String[] args) {
        User user = new User();
        printHash(user);

        user.setId(123);
        printHash(user);

        user.setName("xxx");
        printHash(user);

        user.setName("abcdsfsdf");
        printHash(user);
    }

    static void printHash(User user) {
        System.out.println(user.hashCode() + ": " + Integer.toHexString(System.identityHashCode(user)));
    }

}